﻿using MyOCR.Picture;
using MyOCR.Translate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyOCR
{
    public partial class ShowImage : Form
    {
        Point startPoint;//开始坐标点
        Point endPoint;//结束坐标点
        Point topLeftPoint = new Point();//左上角坐标点
        Point lowerRightPoint = new Point();//右下角坐标点
        Image bg;//保存背景图片

        public ShowImage()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            this.WindowState = FormWindowState.Maximized;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);//设置缓冲避免闪烁 具体参考:https://www.cnblogs.com/arxive/p/6047179.html
        }

        private void ShowImage_Load(object sender, EventArgs e)
        {
            bg = new Bitmap(this.BackgroundImage);
            Clipboard.SetImage(bg);//将原始图片保存到截切版
        }

        //鼠标按下时 保存开始坐标点
        private void ShowImage_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint = Control.MousePosition;
            //Console.WriteLine("down"+ startPoint);
        }

        //鼠标抬起时 保存结束坐标点
        private void ShowImage_MouseUp(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                Image img = this.BackgroundImage;
                Graphics g = Graphics.FromImage(img);

                Size size = new Size(Math.Abs(topLeftPoint.X - lowerRightPoint.X), Math.Abs(topLeftPoint.Y - lowerRightPoint.Y));

                g.CopyFromScreen(topLeftPoint, new Point(0, 0), size);

                Form1 form1 = this.Owner as Form1;
                try
                {
                    Bitmap bitmap = new Bitmap(size.Width, size.Height);//创建新图位图
                    g = Graphics.FromImage(bitmap);
                    g.DrawImage(img, 0, 0, new Rectangle(this.Location, size), GraphicsUnit.Pixel);//截图原图相应区域写入作图
                    img = Image.FromHbitmap(bitmap.GetHbitmap());//生成新图
                }
                catch (Exception ex)
                {
                    
                    this.Close();
                    form1.setTextBox("****************"+ "框选失败" + "****************");
                    form1.Show();
                    return;
                }

                //委托开启线程 调用API进行文字识别  
                //form1.action += Call_OCR_API;

                this.Close();
                form1.OCR_action(img);

                //Action<Image> action = Call_OCR_API;
                //loading.Show();
                //action.BeginInvoke(img, iar => { 
                //    loading.Close();
                //}, null);
            }
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowImage_MouseMove(object sender, MouseEventArgs e)
        {
            endPoint = Control.MousePosition;
            //计算坐标点
            topLeftPoint.X = (startPoint.X <= endPoint.X) ? startPoint.X : endPoint.X;//左上角x点坐标
            topLeftPoint.Y = (startPoint.Y <= endPoint.Y) ? startPoint.Y : endPoint.Y;//左上角y点坐标

            lowerRightPoint.X = (startPoint.X >= endPoint.X) ? startPoint.X : endPoint.X;//右下角x点坐标
            lowerRightPoint.Y = (startPoint.Y >= endPoint.Y) ? startPoint.Y : endPoint.Y;//右下角y点坐标

            if (e.Button == MouseButtons.Left)
            {
                ShowRectangle();
            }
            else
            {
                using (Graphics g = Graphics.FromImage(this.BackgroundImage))
                {
                    //覆盖
                    g.DrawImage(Clipboard.GetImage(), this.Location);

                    Color _MaskColor = Color.FromArgb(125, 0, 0, 0);

                    GraphicsPath gp = new GraphicsPath();
                    gp.AddRectangle(new Rectangle(this.Location, this.Size));
                    using (SolidBrush sb = new SolidBrush(_MaskColor))
                    {
                        g.FillPath(new SolidBrush(_MaskColor), gp);
                    }
                }
            }
        }

        /// <summary>
        /// 绘制矩形选择区
        /// </summary>
        private void ShowRectangle()
        {
            Point _inPoint = Control.MousePosition;

            Size size = new Size(Math.Abs(_inPoint.X - startPoint.X), Math.Abs(_inPoint.Y - startPoint.Y));

            using (Graphics g = Graphics.FromImage(this.BackgroundImage))
            {
                //覆盖
                g.DrawImage(Clipboard.GetImage(), this.Location);

                Point topLeftPoint = new Point();//实时左上角坐标点
                topLeftPoint.X = (startPoint.X <= _inPoint.X) ? startPoint.X : _inPoint.X;//实时左上角坐标点
                topLeftPoint.Y = (startPoint.Y <= _inPoint.Y) ? startPoint.Y : _inPoint.Y;//实时左上角坐标点

                Color _MaskColor = Color.FromArgb(125, 0, 0, 0);

                GraphicsPath gp = new GraphicsPath();
                gp.AddRectangle(new Rectangle(this.Location, this.Size));
                using (SolidBrush sb = new SolidBrush(_MaskColor))
                {
                    gp.AddRectangle(new Rectangle(topLeftPoint, size));
                    g.FillPath(new SolidBrush(_MaskColor), gp);
                }

                g.DrawRectangle(new Pen(Color.Red), new Rectangle(topLeftPoint, size));
            }

            this.Refresh();
        }

        //线程调用方法
        private void Call_OCR_API(Image img)
        {
            Form1 form1 = this.Owner as Form1;
            this.Close();
            form1.OCR_action(img);
            try
            {
                PictureHandle picture = PictureFactory.createPicture(Program._config._Recongnition);
                string msg = picture.IdentifyImage(img);
                form1.setTextBox(msg);
            }
            catch (Exception ex)
            {
                form1.setTextBox("****************" + ex.Message + "****************");
            }
        }

        private void ShowImage_KeyDown(object sender, KeyEventArgs e)
        {
            //空格截图至剪切板
            if(e.KeyCode == Keys.Space)
            {
                Graphics g = Graphics.FromImage(this.BackgroundImage);

                Size size = new Size(Math.Abs(topLeftPoint.X - lowerRightPoint.X), Math.Abs(topLeftPoint.Y - lowerRightPoint.Y));
                Bitmap bitmap = new Bitmap(size.Width, size.Height);//创建新图位图

                g.CopyFromScreen(topLeftPoint, new Point(0, 0), size);
                g = Graphics.FromImage(bitmap);
                g.DrawImage(this.BackgroundImage, 0, 0, new Rectangle(this.Location, size), GraphicsUnit.Pixel);//截图原图相应区域写入作图

                Clipboard.SetImage(bitmap);
                this.Close();
            }
        }

        private void ShowImage_MouseClick(object sender, MouseEventArgs e)
        {
            //鼠标右键按下时 退出识别
            if(e.Button == MouseButtons.Right)
            {
                this.Close();
            }
        }
    }
}
